package org.kong.service.impl;

import org.kong.mapper.StuMapper;
import org.kong.pojo.entity.Classroom;
import org.kong.pojo.entity.Stu;
import org.kong.pojo.entity.Subject;
import org.kong.pojo.entity.Teacher;
import org.kong.pojo.vo.FindAllStuInfoVo;
import org.kong.pojo.vo.StuSubVO;
import org.kong.pojo.vo.StuVO;
import org.kong.pojo.vo.SubjectVO;
import org.kong.service.StuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 加油
 *
 * @author konglingfeng
 * @date 2025/8/15
 * @desc
 */
@Service
public class StuServiceImpl implements StuService {
    @Autowired
    private StuMapper stuMapper;


    @Override
    public Stu searchStuById(int id) {
        return stuMapper.searchStuById(id);
    }

    @Override
    public List<Stu> findAllStu() {
        return stuMapper.findAllStu();
    }

    @Override
    public List<Stu> getStuByCondition(Stu stu) {
        return stuMapper.getStuByCondition(stu);
    }

    @Override
    public boolean addStu(Stu stu) {
        return stuMapper.addStu(stu);
    }

    @Override
    public boolean updateStu(Stu stu) {
        return stuMapper.updateStu(stu);
    }

    @Override
    public boolean deleteStu(int id) {
        return stuMapper.deleteStu(id);
    }

    @Override
    public boolean deleteStuByIds(List<Integer> ids) {
        return stuMapper.deleteStuByIds(ids);
    }

    @Override
    public List<StuVO> getStuClassroom() {
        return stuMapper.getStuClassroom();
    }

    @Override
    public List<StuSubVO> getStuSub() {
        return stuMapper.getStuSub();
    }

    @Override
    public List<FindAllStuInfoVo> findAllInfo() {
        return stuMapper.findAllInfo();
    }

    @Override
    public Classroom getClassroomByCid(Integer cid) {
        return stuMapper.searchClassroomByCid(cid);
    }

    @Override
    public List<SubjectVO> searchSubjectVOBySid(Integer id) {
        List<SubjectVO> subjectVOS = new ArrayList<>();
        List<Subject> subjects = stuMapper.findSubjectBySid(id);
        System.out.println("subjects = " + subjects);
        subjects.forEach(sub->{
            /**
             * 创建subjectVO对象
             */
            SubjectVO subjectVO = new SubjectVO();
            /**
             * 将sub中的数据复制给subjectVO
             */
            BeanUtils.copyProperties(sub, subjectVO);
            /**
             * 通过subId查找对应科目的教师
             */
            Teacher teacher = stuMapper.findTeacherBySubId(sub.getSubId());
            subjectVO.setTeacher(teacher);
            subjectVOS.add(subjectVO);
        });
        System.out.println("subjectVOS = " + subjectVOS);
        return subjectVOS;
    }
}
